<?php

/**
 * @author chenshubo
 * 三西大同供应商
 */
namespace App\Service\OrderService\OrderRouteService;
use App\Service\Code\Normal;
use Service\AppService\IdService;
use App\Model\PssModel\OrderSupplier;
use App\Service\OrderService\BaseSupplierRouterService;
use App\Service\VendorClient\ShopGwApi\ShopGwApiClient;
use App\Service\VendorClient\ShopSanXiDaTong\ShopSanXiDaTongApiClient;
use App\Model\CommodityModel\WarehouseInfo;
use App\Model\CommodityModel\Stockinfo;

class Shopsanxidatong extends BaseSupplierRouterService
{
    /**
     * 创建订单
     * @param array $param
     * @return mixed
     */
    public function createOrder( $param = [] )
    {
        //生成供应商主单号
        $supplierGoodsInfo = $param['supplierGoodsInfo'];
        $supplierCode   = $supplierGoodsInfo['supplierCode'];
        $userGoodsList  = $supplierGoodsInfo['userGoodsList'];
        $apiBody        = $supplierGoodsInfo['apiBody'];        
        $supplierCodes  = array_column($supplierGoodsInfo['goodsList'],'supplierSKUCode');
        
        //关联关务仓库ID
        if( empty($apiBody['CustomWareHouseID']) ) {
            return Normal::returnCode(Normal::FAIL,'请检查关联仓库CustomWareHouseID参数是否配置');
        }
        
        //重新合并每一个仓库商品
        $orderGoods = [];
        foreach ($userGoodsList as $goods ) {
            $orderGoods = array_merge($orderGoods,$goods);
            unset($goods);
        }
        
        $param['order']['goods'] = $orderGoods;
        
        //下游是否申报
        $isDelare = (
            (isset($param['order']['isOrderDeclare']) && $param['order']['isOrderDeclare'] == 1) ||
            (isset($param['order']['isPayDeclare']) && $param['order']['isPayDeclare'] == 1)
            ) ? true : false;
        
        //是否云仓商城
        $isShop = isset($param['order']['isSplitOrder']) && $param['order']['isSplitOrder'] == 1 ? true : false ;
    
        //防止订单号重复问题,下游已申报，则使用下游的订单号作为中台子单号
        if( $isDelare && !$isShop) {
            $param['order']['supplier_dsn'] = $param['order']['orderSn'];
        } else {
            $param['order']['supplier_dsn'] = IdService::instance()->getOtherID();
        }
    
        $param['order']['supplier_psn'] = OrderSupplier::instance()->getCreateOrderSn($supplierCode);
        $param['order']['supplier_csn'] = IdService::instance()->getOtherID();
    
        $res = OrderSupplier::instance()->getSupplierCreateOrderData($param);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        
        //调用上游创建订单接口
        $orderItems[] = $res['data'];
        $param['order']['extends'] = [
            'orderType' => 'sanxidatong',
            'isCallApi' => true
        ];
        
        $wareHouseInfo = WarehouseInfo::instance()->info(['ID'=>$apiBody['CustomWareHouseID']]);
        if( empty($wareHouseInfo['JsonBody']) ) {
            return Normal::returnCode(Normal::FAIL,'关务仓库参数不能为空');
        }
        //查询山西大同关务的数据
        $stockBatchRowList = Stockinfo::instance()->getStockBatchInfoListBySupplierCodesAndWareHouseID($supplierCodes, $apiBody['CustomWareHouseID']);
        if( empty($stockBatchRowList) ) {
            return Normal::returnCode(Normal::FAIL,'关务供应商库存数据不存在');
        }
        
        $apiBody = json_decode($wareHouseInfo['JsonBody'],true);
        $param['supplierGoodsInfo']['stockBatchRowList'] = $stockBatchRowList;
        $res = $this->callSdkApi(ShopGwApiClient::class,$apiBody)->createOrder($param);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'supplierCode' => $supplierCode,
            'orderItems' => $orderItems
        ]);
    }
    
    /**
     * 订单支付
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        $apiBody = $param['apiBody'];
        //如果发起了自主的运单申报
        /* if( !empty($apiBody['ExpressCode']) ) {
            $clientClass = ucfirst($apiBody['ExpressCode']);
            $class = "\\App\\Service\\VendorClient\\WayBillDeclare\\{$clientClass}";
            $res = $this->callSdkApi($class,$param['apiBody'])->logisticDeclare($param);
        } */
        
        //需要跳过申报，默认申报成功
        $param['order']['SkipOrderDelcare']     = true;
        $param['order']['SkipWayBillDeclare']   = true;
        $param['order']['SkipInventoryDeclare'] = true;
        $wareHouseInfo = WarehouseInfo::instance()->info(['ID'=>$param['apiBody']['CustomWareHouseID']]);
        if( empty($wareHouseInfo['JsonBody']) ) {
            return Normal::returnCode(Normal::FAIL,'关务仓库参数不能为空');
        }
        $apiBody = json_decode($wareHouseInfo['JsonBody'],true);
        $res = $this->callSdkApi(ShopGwApiClient::class,$apiBody)->payOrder($param);
        if( $res['code'] != Normal::SUC ) {
            if( ! strpos($res['msg'],'已支付，请勿重复支付') !== false ) {
                return $res;
            }
        }
        return $this->callSdkApi(ShopSanXiDaTongApiClient::class,$param['apiBody'])->createOrder($param);
    }
    
    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        $apiBody = $param['apiBody'];
        $wareHouseInfo = WarehouseInfo::instance()->info(['ID'=>$param['apiBody']['CustomWareHouseID']]);
        if( empty($wareHouseInfo['JsonBody']) ) {
            return Normal::returnCode(Normal::FAIL,'关务仓库参数不能为空');
        }
        $apiBody = json_decode($wareHouseInfo['JsonBody'],true);
        return $this->callSdkApi(ShopGwApiClient::class,$apiBody)->cancelOrder($param);
    }
    
    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale($param = [])
    {
        $apiBody = $param['apiBody'];
        $wareHouseInfo = WarehouseInfo::instance()->info(['ID'=>$param['apiBody']['CustomWareHouseID']]);
        if( empty($wareHouseInfo['JsonBody']) ) {
            return Normal::returnCode(Normal::FAIL,'关务仓库参数不能为空');
        }
        $apiBody = json_decode($wareHouseInfo['JsonBody'],true);
        return $this->callSdkApi(ShopGwApiClient::class,$apiBody)->cancelOrder($param);
    }
}